package com.haidong.component;

import com.haidong.SqlFunction;
import com.haidong.pojo.HouseCost;
import com.haidong.pojo.NormPrice;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

public class CostPanel extends JPanel {
    public static JPanel select(){
        JPanel panel = new JPanel(new BorderLayout());

        JPanel up = new JPanel(new GridLayout(1,11));
        JLabel label = new JLabel("房子id:");
        TextField textField = new TextField();
        JButton button = new JButton("当月查询");
        for (int i = 0; i < 7; i++) {
            up.add(new JLabel(""));
        }
        up.add(label);
        up.add(textField);
        up.add(button);

        JPanel center = new JPanel(new GridLayout(1,2));

        Object[] costHead = new Object[]{"id","房名","物业费"};
        DefaultTableModel tableModel = new DefaultTableModel(new Object[][]{},costHead);
        JTable table = new JTable(tableModel);
        table.setRowHeight(30);
        JScrollPane scrollPane = new JScrollPane(table);
        center.add(scrollPane);

        Object[] normHead = new Object[]{"id","水价/吨","电价/度","燃气价/立方","管理费/平方"};
        DefaultTableModel tableModel2 = new DefaultTableModel(new Object[][]{},costHead);
        JTable table2 = new JTable(tableModel2);
        table2.setRowHeight(30);
        JScrollPane scrollPane2 = new JScrollPane(table2);
        center.add(scrollPane2);

        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String text = textField.getText();
                if("".equals(text)){
                    List<HouseCost> hcs = SqlFunction.selectAllCost();
                    Object[][] data = new Object[hcs.size()][3];
                    for (int i = 0; i < hcs.size(); i++) {
                        data[i] = getSingleCostData(hcs.get(i));
                    }
                    tableModel.setDataVector(data,costHead);
                }else{
                    Object[][] data = new Object[1][3];
                    data[0] = getSingleCostData(SqlFunction.selectByIdCost(Integer.parseInt(text)));
                    tableModel.setDataVector(data,costHead);
                }
                List<NormPrice> nps = SqlFunction.selectAllNormPrice();
                Object[][] data2 = new Object[nps.size()][5];
                for (int i = 0; i < nps.size(); i++) {
                    data2[i] = getSigleNormDate(nps.get(i));
                }
                tableModel2.setDataVector(data2,normHead);
            }
        });
        panel.add(up,BorderLayout.NORTH);
        panel.add(center,BorderLayout.CENTER);

        return panel;
    }
    public static JPanel history(){
        JPanel panel = new JPanel(new BorderLayout());

        JPanel up = new JPanel(new GridLayout(1,6));
        String[] strs = {"所在年:","所在月:","房子id:"};
        JLabel[] labels = new JLabel[3];
        TextField[] textFields = new TextField[3];
        JPanel[] panels = new JPanel[3];
        JButton button = new JButton("查询");
        for (int i = 0; i < 2; i++) {
            up.add(new JLabel(""));
        }
        for (int i = 0; i < 3; i++) {
            labels[i] = new JLabel("");
            labels[i].setText(strs[i]);
            textFields[i] = new TextField();
            panels[i] = new JPanel(new GridLayout(1,2));
            panels[i].add(labels[i]);
            panels[i].add(textFields[i]);
            up.add(panels[i]);
        }
        up.add(button);

        JPanel center = new JPanel(new GridLayout(1,1));
        Object[] costHead = new Object[]{"id","房名","物业费"};
        DefaultTableModel tableModel = new DefaultTableModel(new Object[][]{},costHead);
        JTable table = new JTable(tableModel);
        table.setRowHeight(30);
        JScrollPane scrollPane = new JScrollPane(table);
        center.add(scrollPane);

        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Integer year = "".equals(textFields[0].getText())?null:Integer.valueOf(textFields[0].getText());
                Integer month = "".equals(textFields[1].getText())?null:Integer.valueOf(textFields[1].getText());
                Integer id = "".equals(textFields[2].getText())?null:Integer.valueOf(textFields[2].getText());
                List<HouseCost> hcs = SqlFunction.selectConditionCost(id,year,month);
                Object[][] data = new Object[hcs.size()][3];
                for (int i = 0; i < hcs.size(); i++) {
                    data[i] = getSingleCostData(hcs.get(i));
                }
                tableModel.setDataVector(data,costHead);
            }
        });

        panel.add(up,BorderLayout.NORTH);
        panel.add(center,BorderLayout.CENTER);

        return panel;
    }

    public static JPanel update(){
        JPanel panel = new JPanel(new GridLayout(1,3));
        JPanel upper = new JPanel(new GridLayout(12,3));
        JLabel labelId = new JLabel("请输入id(此为索引):");
        labelId.setFont(new Font("dialog",0,20));
        TextField textFieldId = new TextField();
        textFieldId.setFont(new Font("dialog",0,25));
        JPanel panelId = new JPanel(new GridLayout(1,2));
        panelId.add(labelId);
        panelId.add(textFieldId);
        JLabel[] labels = {new JLabel("请输入水价:"),new JLabel("请输入电价:"),
                new JLabel("请输入燃气价:"),new JLabel("请输入管理价:")};
        TextField[] textField = new TextField[4];
        JPanel[] panels = new JPanel[4];
        for (int i = 0; i < 4; i++) {
            textField[i] = new TextField();
            panels[i] = new JPanel(new GridLayout(1,2));
            labels[i].setFont(new Font("Dialog",0,20));
            textField[i].setFont(new Font("Dialog",0,25));
            panels[i].add(labels[i]);
            panels[i].add(textField[i]);
        }

        JButton button = new JButton("确认修改");
        button.setFont(new Font("Dialog",0,25));
        JLabel label = new JLabel("");
        label.setFont(new Font("Dialog",0,25));
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                NormPrice np = new NormPrice();
                np.setId(Integer.valueOf(textFieldId.getText()));
                np.setWaterPrice("".equals(textField[0].getText())?null:Double.valueOf(textField[0].getText()));
                np.setElectPrice("".equals(textField[1].getText())?null:Double.valueOf(textField[1].getText()));
                np.setGasPrice("".equals(textField[2].getText())?null:Double.valueOf(textField[2].getText()));
                np.setPrice("".equals(textField[3].getText())?null:Double.valueOf(textField[3].getText()));
                SqlFunction.updateNormPrice(np);
                label.setText("修改成功!");
            }
        });

        upper.add(new JLabel(""));
        upper.add(panelId);
        upper.add(new JLabel(""));
        for (int i = 0; i < 3; i++) {upper.add(new JLabel(""));}
        upper.add(panels[0]);
        upper.add(new JLabel(""));
        upper.add(panels[1]);
        for (int i = 0; i < 3; i++) {upper.add(new JLabel(""));}
        upper.add(panels[2]);
        upper.add(new JLabel(""));
        upper.add(panels[3]);
        for (int i = 0; i < 10; i++) {upper.add(new JLabel(""));}
        upper.add(button);
        for (int i = 0; i < 5; i++) {upper.add(new JLabel(""));}
        upper.add(label);
        for (int i = 0; i < 4; i++) {upper.add(new JLabel(""));}

        panel.add(upper);
        return panel;
    }

    public static Object[] getSingleCostData(HouseCost hc){
        return new Object[]{hc.getId(),hc.getHouseName(),hc.getCost()};
    }
    public static Object[] getSigleNormDate(NormPrice np){
        return new Object[]{np.getId(),np.getWaterPrice(),np.getElectPrice(),np.getGasPrice(),np.getPrice()};
    }
}
